﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Service
{
    public partial class UserLogin : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string loginname = Request["loginname"] ?? "";
            string password = Request["password"] ?? "";
            string secretkey = Request["secretkey"] ?? "";

            if (!IsValidKey(secretkey, loginname, password))
            {
                Response.Write("112");//密钥不正确 
                return;
            }

            if (Request["loginname"] != null && Request["password"] != null)
            {
                Model.UserInfo user = BLL.UserInfoBLL.Instance.Login(loginname,password);
                if (user != null)
                {
                    if (user.IsDel == (int)Common.Enum.auvDel.Del)
                    {
                        Response.Write("212");//用户已被禁用
                    }
                    //else if (user.State == (int)Common.Enum.auvState.Disable)
                    //{
                    //    Response.Write("210");//用户未激活
                    //}
                    else
                    {
                        Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(user));//登录成功返回用户对象
                    }
                }
                else
                {
                    //用户名或密码错误
                    Response.Write("211");
                }
            }
            else
            {
                Response.Write("111");
            }
        }

        private bool IsValidKey(string secretkey, string loginname, string password)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey = base.EncryptMD5(public_key + current_day + loginname + password).Substring(6, 22);
                    return secretkey == finalkey;
                }
            }
            else
            {
                return true;
            }
        }
    }
}